package cn.cloudplug.core.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.ContentFrameLayout;

import cn.cloudplug.core.R;
import me.yokeyword.fragmentation.ExtraTransaction;
import me.yokeyword.fragmentation.ISupportActivity;
import me.yokeyword.fragmentation.SupportActivityDelegate;
import me.yokeyword.fragmentation.anim.FragmentAnimator;

/**
 * Created by pc on 2018/1/18.
 */

public abstract class BaseActivity extends AppCompatActivity implements ISupportActivity {
    private final SupportActivityDelegate SUPPORT = new SupportActivityDelegate(this);

    public abstract BaseDelegate setRootDelegate();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SUPPORT.onCreate(savedInstanceState);
        initContainer(savedInstanceState);
    }

    private void initContainer(@Nullable Bundle savedInstanceState) {
        final ContentFrameLayout container = new ContentFrameLayout(this);
        container.setId(R.id.delegate_container);
        setContentView(container);
        if (savedInstanceState == null) {
            SUPPORT.loadRootFragment(R.id.delegate_container, setRootDelegate());
        }
    }

    @Override
    protected void onDestroy() {
        SUPPORT.onDestroy();
        super.onDestroy();
        System.gc();
        System.runFinalization();
    }

    @Override
    public SupportActivityDelegate getSupportDelegate() {
        return SUPPORT;
    }

    @Override
    public ExtraTransaction extraTransaction() {
        return SUPPORT.extraTransaction();
    }

    @Override
    public FragmentAnimator getFragmentAnimator() {
        return SUPPORT.getFragmentAnimator();
    }

    @Override
    public void setFragmentAnimator(FragmentAnimator fragmentAnimator) {
        SUPPORT.setFragmentAnimator(fragmentAnimator);
    }

    @Override
    public FragmentAnimator onCreateFragmentAnimator() {
        return SUPPORT.onCreateFragmentAnimator();
    }

    @Override
    public void post(Runnable runnable) {
        SUPPORT.post(runnable);
    }

    @Override
    public void onBackPressedSupport() {
        SUPPORT.onBackPressedSupport();
    }

    @Override
    public void onBackPressed() {
        SUPPORT.onBackPressed();
    }
}
